package com.zq.service.impl;

import com.zq.entity.User;
import com.zq.repository.UserRepository;
import com.zq.service.UserService;
import com.zq.utils.FenyeUtil;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.awt.print.Pageable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:任人渐疏
 * @Date:Created in 11:25 上午 2020/6/26
 * Update:true
 */
@Service
public class UserServiceImpl  implements UserService {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private  UserService userService;
    @Override
    public FenyeUtil<User> findAll(User user, Integer page) {
        FenyeUtil<User> f=new FenyeUtil<>();
        PageRequest of = PageRequest.of(page, 10);
        Page<User> pageList=userRepository.findAll(get(user),of);
        List<User> content = pageList.getContent();
        long totalElements = pageList.getTotalElements();
        f.setCount((int) totalElements);
        f.setData(content);
        f.setCode(0);
        f.setMsg("");
        return f;
    }

    private Specification<User> get(User user) {
        return new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate conjunction = criteriaBuilder.conjunction();
                List<Expression<Boolean>> expressions = conjunction.getExpressions();
                if(!StringUtils.isEmpty(user.getNicheng())){
                    expressions.add(criteriaBuilder.like(root.get("nicheng"),"%"+user.getNicheng()+"%"));
                }
                if(!StringUtils.isEmpty(user.getType())){
                    expressions.add(criteriaBuilder.equal(root.get("type"),user.getType()));
                }
                return conjunction;
            }
        };
    }

    @Override
    public User findByUname(String yongHuMing) {
        return userRepository.findByUname(yongHuMing);
    }

    @Override
    public String updateUser(User user) {
       user.setUpdatetime(sdf.format(new Date()));
        try {
            userRepository.saveAndFlush(user);
            return "修改成功";
        }catch (Exception e){
            return "修改失败";
        }

    }

    @Override
    public String addUser(User user) {
        user.setCreatetime(sdf.format(new Date()));
        try {
            userRepository.save(user);
            return "添加成功";
        }catch (Exception e){
            return "添加失败";
        }

    }

    @Override
    public String deleteUser(String uids) {
        try {
        String[] split = uids.split(",");
        for (int i = 0; i <split.length ; i++) {
            Integer id=Integer.parseInt(split[i]);
            userRepository.deleteById(id);
        }
            return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }

    }
}
